package cn.ichiva.luckysheet.utils;


import ch.qos.logback.core.util.TimeUtil;
import cn.ichiva.luckysheet.enenty.Ct;
import cn.ichiva.luckysheet.enenty.V;
import cn.ichiva.luckysheet.enenty.config.*;
import cn.ichiva.luckysheet.enenty.group.GroupJsonRoot;
import cn.ichiva.luckysheet.enenty.group.GroupRange;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

/**
 * TODO
 *
 * @author pzq
 * @version 1.0
 * @date 2022/1/4 16:45
 */
public class Jimubaobiao {
    private static Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
//    public static void main(String[] args) {
public static void main(String[] args) {
    selinfo("id=640709740980563969&apiUrl=&params=");
    System.out.println("13");
}
public static String selinfo(String id) {
        String token="token=null";
        String str =  HttpUtils.sendGet("http://localhost/jmreport/get/640709740980563969",token);
        JSONObject jsonObjectStr  = JSONObject.parseObject(str);
        JSONObject js =(JSONObject) jsonObjectStr.get("result");
        String json=js.get("jsonStr").toString();
//        System.out.println(json); 640709740980563968  638281735431733248 641417146249072640
        //String id="id=638281735431733248&apiUrl=&params=";
//        String id="id=640709740980563969&apiUrl=&params=";
        String pamer="{\"token\":\"null\",\"pageNo\":1,\"pageSize\":10,\"jmViewFirstLoad\":\"1\"}";
//        String pamer="{\"token\":\"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NDIwNDQ1NDMsInVzZXJuYW1lIjoiMTM4NzE1NDExMjMifQ.SCfqgvye7BnS6eiu5DoMZxSWyrLEne339Zj0kPjBwIk\",\"pageNo\":1,\"pageSize\":10,\"jmViewFirstLoad\":\"1\"}";
        pamer=URLEncoder.encode(pamer);
        String str2= HttpUtils.sendGet("http://localhost/jmreport/show",id+pamer);
        JSONObject jsonObjectStr2  = JSONObject.parseObject(str2);
        JSONObject js2 =(JSONObject) jsonObjectStr2.get("result");
        String json2=js2.get("jsonStr").toString();
        JSONObject jsonObjectStr3  = JSONObject.parseObject(json2);
        Map<String,Object> rows=(Map<String, Object>) jsonObjectStr3.get("rows");
        List<Map<String,Object>> styles=(List<Map<String,Object>>) jsonObjectStr3.get("styles");
        Set<String> row= rows.keySet();
        JSONObject jsonObjectkey = new JSONObject();
        JSONArray jsonObjectList = new JSONArray();
        List<JsonRootBean> jsonRootBeanlist = new ArrayList<>();//样式实体类
        List<GroupJsonRoot> groupJsonRootList= new ArrayList<>();//合并单元格样式
        for (Iterator iterator = row.iterator(); iterator.hasNext();) {
            String string = (String) iterator.next();
//            System.out.println(string);
            String r=rows.get(string).toString();
            if("len".equals(string)){
                continue;
            }
            JSONObject jsonObjectStr4  = JSONObject.parseObject(r);
            Map<String,Object> cells=(Map<String, Object>)jsonObjectStr4.get("cells");
            Set<String> cell= cells.keySet();
            for (Iterator iterator1 = cell.iterator(); iterator1.hasNext();) {
                String string1 = (String) iterator1.next();
                String r1=cells.get(string1).toString();
                JSONObject parseObject  = JSONObject.parseObject(r1);
                JSONObject json1 = new JSONObject();
                V v2 = new V();
                json1.put("r",Integer.valueOf(string));
                json1.put("c",Integer.valueOf(string1));
//                v2.setBg();
                json1.put("v",v2);
                if(ObjectUtils.isNotEmpty(parseObject.get("style"))){

                   int num=(int) Integer.valueOf(parseObject.get("style").toString());
                     Map<String,Object> mp=styles.get(num);
                     String bgcolor=ObjectUtils.getObject2String(mp.get("bgcolor"));//判断单元格颜色
                     String align=ObjectUtils.getObject2String(mp.get("align"));//判断单元格位置
                     if(ObjectUtils.isNotEmpty(align)){
                         System.out.println(align);
                         if("center".equals(align)){
                             v2.setVt("0");
                             v2.setHt("0");
                         }
                     }
                     if(ObjectUtils.isNotEmpty(bgcolor)){
                         System.out.println(bgcolor);
                         v2.setBg(bgcolor);
                     }

                    JSONObject font=(JSONObject)mp.get("font");//判断字体样式
                    if(ObjectUtils.isNotEmpty(font)){
                        String bold=ObjectUtils.getObject2String(font.get("bold"));
                        String size=ObjectUtils.getObject2String(font.get("size"));
                        if(ObjectUtils.isNotEmpty(bold)){
                            if("true".equals(bold)){
                                v2.setBl("1");
                            }
                        }
                        if(ObjectUtils.isNotEmpty(size)){
                            v2.setFs(size);
                        }
                    }
                     JSONObject border=(JSONObject)mp.get("border");
                     if(ObjectUtils.isNotEmpty(border)){
                         JsonRootBean jsonRootBean = new JsonRootBean();
                         Value value =new Value();
                         jsonRootBean.setRangeType("cell");
                         value.setCol_index(Integer.valueOf(string1));
                         value.setRow_index(Integer.valueOf(string));

                         if(ObjectUtils.isNotEmpty(border.get("top"))){
                             JSONArray jsonArray=(JSONArray)border.get("top");
                             T t = new T();
                             t.setColor(jsonArray.get(1).toString());
                             value.setT(t);
                         }
                         if(ObjectUtils.isNotEmpty(border.get("left"))){
                             JSONArray jsonArray=(JSONArray)border.get("left");
                             L l = new L();
                             l.setColor(jsonArray.get(1).toString());
                             value.setL(l);
                         }
                         if(ObjectUtils.isNotEmpty(border.get("bottom"))){
                             JSONArray jsonArray=(JSONArray)border.get("bottom");
                             B b = new B();
                             b.setColor(jsonArray.get(1).toString());
                             value.setB(b);
                         }
                         if(ObjectUtils.isNotEmpty(border.get("right"))){
                             JSONArray jsonArray=(JSONArray)border.get("right");
                             R rr = new R();
                             rr.setColor(jsonArray.get(1).toString());
                             value.setR(rr);
                         }
                         if(ObjectUtils.isNotEmpty(parseObject.get("merge"))){
                             JSONArray jsonArray=(JSONArray)border.get("bottom");
                             JSONArray meg=(JSONArray)parseObject.get("merge");
                             GroupJsonRoot groupJsonRoot=new GroupJsonRoot();
                             groupJsonRoot.setColor(jsonArray.get(1).toString());
                             List<GroupRange> groupRangeList=new ArrayList<>();
                             GroupRange groupRange = new GroupRange();
                             List<Integer> groupr=new ArrayList<>();
                             List<Integer> groupc=new ArrayList<>();
                             groupr.add(Integer.valueOf(string));
                             groupr.add((int)meg.get(0)+Integer.valueOf(string));
                             groupc.add(Integer.valueOf(string1));
                             groupc.add((int)meg.get(1)+Integer.valueOf(string1));
                             groupRange.setColumn(groupc);
                             groupRange.setRow(groupr);
                             groupRangeList.add(groupRange);
                             groupJsonRoot.setRange(groupRangeList);

                             groupJsonRootList.add(groupJsonRoot);
                         }
                         jsonRootBean.setValue(value);
                         jsonRootBeanlist.add(jsonRootBean);
                     }

                }
                //判断是否又需要合并的单元格
                if(ObjectUtils.isNotEmpty(parseObject.get("merge"))){
                    JSONArray meg=(JSONArray)parseObject.get("merge");

                    JSONObject jsonObjectmerge = new JSONObject();

                    String key = string+"_"+string1;
                    jsonObjectmerge.put("r",Integer.valueOf(string));
                    jsonObjectmerge.put("c",Integer.valueOf(string1));
                    jsonObjectmerge.put("rs",(int)meg.get(0)+1);
                    jsonObjectmerge.put("cs",(int)meg.get(1)+1);
                    jsonObjectkey.put(key,jsonObjectmerge);
                }
                v2.setM(ObjectUtils.getObject2String(parseObject.get("text")));
                v2.setV(ObjectUtils.getObject2String(parseObject.get("text")));
                Ct ct = new Ct();
                ct.setFa("General");
                ct.setT("g");
                v2.setCt(ct);
                jsonObjectList.add(json1);
//                System.out.println(json1);
            }
//            System.out.println(rows.get(string));
        }

        Map<String, Object> resultMap = sortMapByKey(rows);
//        JSONArray rows=(JSONArray)json2.get("rows");
        System.out.println("数据json");
        System.out.println(jsonObjectList.toString());
        System.out.println("合并单元格json");
        System.out.println(jsonObjectkey.toString());
        System.out.println("样式json");
        System.out.println(JSON.toJSONString(jsonRootBeanlist));
        System.out.println("合并单元格样式");
        System.out.println(JSON.toJSONString(groupJsonRootList));
        System.out.println(1);
//        TimeUnit.DAYS.sleep(1);
        return jsonObjectList.toString();
    }
    /**
     * 使用 Map按key进行排序
     * @param map
     * @return
     */
    public static Map<String, Object> sortMapByKey(Map<String, Object> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }

        Map<String, Object> sortMap = new TreeMap<String, Object>(
                new MapKeyComparator());

        sortMap.putAll(map);

        return sortMap;
    }
    static class MapKeyComparator implements Comparator<String> {

        @Override
        public int compare(String str1, String str2) {

            return str1.compareTo(str2);
        }
    }
    public static String analysis(String api){
        String json="";
        String str2= HttpUtils.doPost("http://localhost/jmreport/show",json,null);
        return str2;
    }
}
